import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:intl/intl.dart' as intl;

class Utils {
  static hideTextInput() async {
    SystemChannels.textInput.invokeMethod('TextInput.hide');
  }

  ///计算文本的显示大小
  static Size boundingTextSize(BuildContext context, String? text, TextStyle style,  {int maxLines = 2^31, double maxWidth = double.infinity}) {
    if (text != null && text.isEmpty) {
      return Size.zero;
    }
    final TextPainter textPainter = TextPainter(
        textDirection: TextDirection.ltr,
        locale: Localizations.localeOf(context),
        text: TextSpan(text: text, style: style), maxLines: maxLines)
      ..layout(maxWidth: maxWidth);
    return textPainter.size;
  }

  ///处理url编码
  static String urlEncode(String url) {
    int index = url.lastIndexOf('/');
    String preFix = url.substring(0, index + 1);
    String subFix = url.substring(index + 1);
    var encodeSubFix = Uri.encodeComponent(subFix).toString();
    String urlEncode = preFix + encodeSubFix;
    return urlEncode;
  }

  ///获取系统包资源
  static Future<PackageInfo> getPackageInfo() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    return packageInfo;
  }

  ///字符数字格式化显示
  static String numberFormatString(double num,{int digit = 2,String? currency,bool? currentRight}){
    String digitStr = "";
    if(digit>0){
      digitStr = ".";
    }
    for(int i=0;i<digit;i++){
      digitStr += "0";
    }
    intl.NumberFormat numFormat = intl.NumberFormat('###,#0$digitStr', 'en_US');
    String moneyString =  numFormat.format(num);
    if (currency != null && currency.isNotEmpty == true) {
      if(currentRight == true) {
        moneyString =  moneyString + currency;
      }else{
        moneyString = currency + moneyString;
      }
    }
    return moneyString;
  }

  ///验证邮箱
  static bool verifyEmail(String email) {
    if (email.isEmpty) return false;
    // 邮箱正则
    String regexEmail = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*\$";
    return RegExp(regexEmail).hasMatch(email);
  }


}


